package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.goods.bo.CreateGoodsBo;
import com.cskaoyan.bean.goods.vo.BrandListBean;
import com.cskaoyan.bean.goods.vo.CatAndBrandVo;
import com.cskaoyan.bean.goods.vo.CategoryListBean;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;


    /**
     * 商品回显信息
     *
     * @param basePageInfo
     * @return
     */
    @Override
    public CommonData<MarketGoods> queryAllGoods(BasePageInfo basePageInfo, String name, Integer goodsId, String goodSn) {
        // 为了应付admin/coupon的一个莫名其妙请求添加了下面的if语句
        if(basePageInfo.getPage()==null&&basePageInfo.getLimit()==0) {
            CommonData<MarketGoods> marketGoodsCommonData = new CommonData<>();
            return marketGoodsCommonData;
        }
        // 执行查询之前 → 使用PageHelper开启分页就可以了
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketGoodsExample example = new MarketGoodsExample();
        example.setDistinct(true);
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        MarketGoodsExample.Criteria criteria = example.createCriteria();

//        MarketGoods marketGoods = new MarketGoods();
//        marketGoods.setId(goodsId);

        criteria.andDeletedEqualTo(false);

        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andIdEqualTo(goodsId);
        }

        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (goodSn != null && !"".equals(goodSn)) {
            criteria.andGoodsSnLike("%" + goodSn + "%");
        }


        //在数据库里面查询list
        List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExampleWithBLOBs(example);

        //给要返回的对象传入数据（list和4个数据），做手脚
        CommonData<MarketGoods> marketGoodsCommonData = new CommonData<>();
        marketGoodsCommonData.setLimit(basePageInfo.getLimit());
        marketGoodsCommonData.setPage(basePageInfo.getPage());
        //这些是要查完数据库才有的数据
        PageInfo pageInfo = new PageInfo(marketGoodsList);
        Integer total = (int) pageInfo.getTotal();
        int pages = pageInfo.getPages();
        //继续赋值
        marketGoodsCommonData.setPages(pages);
        marketGoodsCommonData.setTotal(total);
        marketGoodsCommonData.setList(marketGoodsList);
        return marketGoodsCommonData;
    }

    /**
     * 商品的分类和品牌下拉种类选择
     *
     * @return
     */
    @Override
    public CatAndBrandVo  queryCatAndBrand() {
        //创建两个Example
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();

        //在数据库里面查询数据
        List<MarketBrand> brandsList = marketBrandMapper.selectByExample(marketBrandExample);
        LinkedList<BrandListBean> res = new LinkedList<>();
        for (MarketBrand marketBrand : brandsList) {
            BrandListBean brandListBean = new BrandListBean();
            brandListBean.setLabel(marketBrand.getName());
            brandListBean.setValue(marketBrand.getId());
            res.add(brandListBean);
        }


        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        //查询父ID
        criteria.andPidEqualTo(0);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        //查询children并且给两个变量赋值
        LinkedList<CategoryListBean> categoryListBeans = new LinkedList<>();
        for (MarketCategory marketCategory : marketCategories) {
            CategoryListBean categoryListBean = new CategoryListBean();
            categoryListBean.setValue(marketCategory.getId());
            categoryListBean.setLabel(marketCategory.getName());


            MarketCategoryExample marketCategoryExample2 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria2 = marketCategoryExample2.createCriteria();
            criteria2.andPidEqualTo(marketCategory.getId());

            List<MarketCategory> childrenCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample2);
            LinkedList<CategoryListBean.Children> childrenLinkedList = new LinkedList<>();
            for (MarketCategory category : childrenCategories1) {
                CategoryListBean.Children children = new CategoryListBean.Children();
                children.setValue(category.getId());
                children.setLabel(category.getName());
                childrenLinkedList.add(children);
            }
            categoryListBean.setChildren(childrenLinkedList);
            categoryListBeans.add(categoryListBean);
        }
        //给要返回的对象赋值，字段是对应的不一样
        return new CatAndBrandVo(categoryListBeans, res);
    }

    /**
     * 新增商品
     *
     * @param createGoodsBo
     * @return
     */
    @Override
    public int createGoods(CreateGoodsBo createGoodsBo) {
        int code = 0;

        //1.插入goods
        MarketGoods goods = createGoodsBo.getGoods();
        code = marketGoodsMapper.insertSelective(goods);
        if (code != 1) {
            return 0;
        }
        code = 0;

        //2.先插入attributes
        List<MarketGoodsAttribute> attributes = createGoodsBo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            code = marketGoodsAttributeMapper.insertSelective(attribute);
        }
        if (code != 1) {
            return 0;
        }
        code = 0;

        //3.插入specification
        List<MarketGoodsSpecification> specifications = createGoodsBo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            code = marketGoodsSpecificationMapper.insertSelective(specification);
        }
        if (code != 1) {
            return 0;
        }
        code = 0;




        //4.插入Products
        List<MarketGoodsProduct> products = createGoodsBo.getProducts();
        for (MarketGoodsProduct product : products) {
            product.setId(goods.getId());
            code = marketGoodsProductMapper.insertSelective(product);
        }
        if (code != 1) {
            return 0;
        }
        return 1;
    }

    @Override
    public int updateGoods(CreateGoodsBo createGoodsBo) {
        int code = 0;

        //1.先更新attributes
        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        List<MarketGoodsAttribute> attributes = createGoodsBo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
//            code = marketGoodsAttributeMapper.updateByExampleSelective(attribute, marketGoodsAttributeExample);
            code = marketGoodsAttributeMapper.updateByPrimaryKeySelective(attribute);
        }
        if (code != 1) {
            return 0;
        }
        code = 0;
        //2.插入specification

        List<MarketGoodsSpecification> specifications = createGoodsBo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            code = marketGoodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }

        if (code != 1) {
            return 0;
        }
        code = 0;

        //3.插入goods
        MarketGoods goods = createGoodsBo.getGoods();

        code = marketGoodsMapper.updateByPrimaryKeySelective(goods);
        if (code != 1) {
            return 0;
        }
        code = 0;

        //4.插入Products
        List<MarketGoodsProduct> products = createGoodsBo.getProducts();
        for (MarketGoodsProduct product : products) {
            code = marketGoodsProductMapper.updateByPrimaryKeySelective(product);
        }
        if (code != 1) {
            return 0;
        }
        return 1;
    }

    @Override
    public int deleteGoods(MarketGoods marketGoods) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();

        marketGoods.setDeleted(true);
        marketGoodsExample.createCriteria().andIdEqualTo(marketGoods.getId());
        //更新
        int code = marketGoodsMapper.updateByExampleSelective(marketGoods, marketGoodsExample);
        if (code != 1) {
            return 0;
        }
        return 1;
    }

    @Override
    public CreateGoodsBo showAllDetails(Integer id) {
        CreateGoodsBo createGoodsBo = new CreateGoodsBo();

        //1.attributes
        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria = marketGoodsAttributeExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByExample(marketGoodsAttributeExample);
            //赋值
            createGoodsBo.setAttributes(marketGoodsAttributes);

        //2.goods
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
            //赋值
            createGoodsBo.setGoods(marketGoods);

        //3.products
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        marketGoodsProductExample.createCriteria().andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(marketGoodsProductExample);
            //赋值
            createGoodsBo.setProducts(marketGoodsProducts);

        //4.specifications
        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        marketGoodsSpecificationExample.createCriteria().andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByExample(marketGoodsSpecificationExample);
            //赋值
            createGoodsBo.setSpecifications(marketGoodsSpecifications);

        //5.categoryIds
        Integer categoryId = marketGoods.getCategoryId();//子ID
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(categoryId);
        Integer pid = marketCategory.getPid();//父ID
            //赋值
        createGoodsBo.setCategoryIds(new Integer[]{pid, categoryId});

        return createGoodsBo;

    }
}
